package com.yantong.utils;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.*;

public class ObjectUtils {
	 private static final String JAVAP = "java.";
	    private static final String JAVADATESTR = "java.util.Date";

	    /**
	     * 获取利用反射获取类里面的值和名称
	     *
	     * @param obj
	     * @return
	     * @throws IllegalAccessException
	     */
	    public static Map<String, Object> objectToMap(Object obj) throws IllegalAccessException {
	        Map<String, Object> map = new HashMap<>();
	        Class<?> clazz = obj.getClass();
	        //System.out.println(clazz);
	        for (Field field : clazz.getDeclaredFields()) {
	            field.setAccessible(true);
	            String fieldName = field.getName();
	            Object value = field.get(obj);
	            map.put(fieldName, value);
	        }
	        return map;
	    }

	    /**
	     * 利用递归调用将Object中的值全部进行获取
	     *
	     * @param timeFormatStr 格式化时间字符串默认<strong>2017-03-10 10:21</strong>
	     * @param obj           对象
	     * @param excludeFields 排除的属性
	     * @return
	     * @throws IllegalAccessException
	     */
	    public static Map<String, String> objectToMapString(String timeFormatStr, Object obj, String... excludeFields) throws IllegalAccessException {
	        Map<String, String> map = new HashMap<>();

	        if (excludeFields.length!=0){
	            List<String> list = Arrays.asList(excludeFields);
	            objectTransfer(timeFormatStr, obj, map, list);
	        }else{
	            objectTransfer(timeFormatStr, obj, map,null);
	        }
	        return map;
	    }


	    /**
	     * 递归调用函数
	     *
	     * @param obj           对象
	     * @param map           map
	     * @param excludeFields 对应参数
	     * @return
	     * @throws IllegalAccessException
	     */
	    private static Map<String, String> objectTransfer(String timeFormatStr, Object obj, Map<String, String> map, List<String> excludeFields) throws IllegalAccessException {
	        boolean isExclude=false;
	        //默认字符串
	        String formatStr = "YYYY-MM-dd HH:mm:ss";
	        //设置格式化字符串
	        if (timeFormatStr != null && !timeFormatStr.isEmpty()) {
	            formatStr = timeFormatStr;
	        }
	        if (excludeFields!=null){
	            isExclude=true;
	        }
	        Class<?> clazz = obj.getClass();
	        //获取值
	        for (Field field : clazz.getDeclaredFields()) {
	            String fieldName = clazz.getSimpleName() + "." + field.getName();
	            //判断是不是需要跳过某个属性
	            if (isExclude&&excludeFields.contains(fieldName)){
	                continue;
	            }
	            //设置属性可以被访问
	            field.setAccessible(true);
	            Object value = field.get(obj);
	            Class<?> valueClass = value.getClass();
	            if (valueClass.isPrimitive()) {
	                map.put(fieldName, value.toString());

	            } else if (valueClass.getName().contains(JAVAP)) {//判断是不是基本类型
	                if (valueClass.getName().equals(JAVADATESTR)) {
	                    //格式化Date类型
	                    SimpleDateFormat sdf = new SimpleDateFormat(formatStr);
	                    Date date = (Date) value;
	                    String dataStr = sdf.format(date);
	                    map.put(fieldName, dataStr);
	                } else {
	                    map.put(fieldName, value.toString());
	                }
	            } else {
	                objectTransfer(timeFormatStr, value, map,excludeFields);
	            }
	        }
	        return map;
	    }
	    
	    
	    
	    /**
	     * 将url参数转换成map
	     *
	     * @param param aa=11&bb=22&cc=33
	     * @return
	     */
	    public static Map<String, Object> getUrlParams(String param) {
	        Map<String, Object> map = new HashMap<String, Object>(0);
	        if (org.springframework.util.StringUtils.isEmpty(param)) {
	            return map;
	        }
	        String[] params = param.split("|");
	        for (int i = 0; i < params.length; i++) {
	            String[] p = params[i].split("=");
	            if (p.length == 2) {
	                map.put(p[0], p[1]);
	            }
	        }
	        return map;
	    }
	 
	    /**
	     * 将map转换成url
	     *
	     * @param map
	     * @return
	     */
	    public static String getUrlParamsByMap(Map<String, Object> map) {
	        if (map == null) {
	            return "";
	        }
	        StringBuffer sb = new StringBuffer();
	        for (Map.Entry<String, Object> entry : map.entrySet()) {
	            sb.append(entry.getKey() + "=" + entry.getValue());
	            sb.append("|");
	        }
	        String s = sb.toString();
	        return s;
	    }

}
